const express = require('express')
const router = express.Router()
module.exports = router;

// SSR
const mysql = require('../db/mysql')
router.get('/goodslist',async (req,res)=>{
    let sql = `select * from goods limit 0,10`;
    const data = await mysql.query(sql)

    const html = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <h1>商品列表</h1>
        <div class="goodslist>
        <ul>
            ${
                data.map(item=>{
                    return `
                        <li>
                            <p class="id">${item.id}</p>
                            <h4>${item.name}</h4>
                            <p class="price">${item.price}</p>
                        </li>
                    `
                }).join('')
            }
            </ul>
        </div>
    </body>
    </html>
    `
    res.send(html)
})

router.get('/goodslist2',async (req,res)=>{
    let sql = `select * from goods limit 0,10`;
    const data = await mysql.query(sql)

    res.render(
        'goodslist', // 指定渲染模板
        {datalist:data,msg:'hello'}, // 给模板传递数据
    )
})